using System.Collections.Generic;
using CardGameObject.Common;
using Entity;
using Mapper;
using UnityEngine;

namespace CardGameObject.PackageStoreScene
{
    public class PackageManager : MonoBehaviour
    {
        public GameObject PackagePrefab;

        private List<GameObject> positionList = new List<GameObject>();
        private List<GameObject> packageList = new List<GameObject>();
    
        private const int PACKAGE_NUMBER = 10;

        private static PackageManager instance;

        private static int current = 1;
        private static int pageSize = 10;

        private int curPurchasePackageId;
        private GameObject curPurchasePackage;

        public GameObject CoinItem;
        private void Awake()
        {
            if (instance == null)
            {
                instance = this;
            }
        }

        private void Start()
        {
            for (int i = 1; i <= PACKAGE_NUMBER; i++)
            {
                GameObject collectPosition = GameObject.Find("PackagePosition_" + i);
                positionList.Add(collectPosition);
            }
            ShowPackages(current , pageSize);
        }

        public static PackageManager GetInstance()
        {
            return instance;
        }

        public GameObject CreateObjectOnPosition(GameObject position)
        {
            Transform positionTransform = position.transform;
            Vector3 positionPosition = positionTransform.position;
            GameObject newPackage = Instantiate(PackagePrefab, positionPosition, positionTransform.rotation);
            return newPackage;
        }
        
        
        public void ShowPackages(int current , int pageSize)
        {
            ClearPackages();
            current = current <= 0 ? 1 : current;
            PackageMapper packageMapper = new PackageMapper();
            List<Package> packages = packageMapper.ListPackagesPage(current, pageSize);
            for(int i = 0; i<packages.Count; i++)
            {
                GameObject package = CreateObjectOnPosition(positionList[i]);
                StorePackage storePackage = package.GetComponent<StorePackage>();
                storePackage.SetPackageInfo(packages[i].Id , packages[i].Name, packages[i].Price);
                packageList.Add(package);
            }
            packageMapper.CloseConnection();
        }

        public void OnShowNextPagePackages()
        {
            current++;
            ShowPackages(current , pageSize);        
        }

        public void OnShowPrePagePackages()
        {
            current = current <= 0 ? 1 : current-1;
            ShowPackages(current, pageSize);
        }

        public void OnExit()
        {
            GlobalSceneManager.LoadSceneByName("MainScene");
        }

        public void SetCurPurchasePackage(GameObject package , int packageId)
        {
            if (curPurchasePackage != null)
            {
                StorePackage oldPackage = curPurchasePackage.GetComponent<StorePackage>();
                oldPackage.SetBorderTransparent();
            }
            curPurchasePackage = package;
            curPurchasePackageId = packageId;
            StorePackage newPackage = curPurchasePackage.GetComponent<StorePackage>();
            newPackage.SetSelectedBorder();
        }

        public void OnPurchase()
        {
            if (curPurchasePackageId == 0)
            {
                Debug.Log("请先选择要购买的卡包");
                return;
            }
            ItemDisplay coinItemDisplay = CoinItem.GetComponent<ItemDisplay>();
            coinItemDisplay.ChangeValue(-100);
            UserMapper userMapper = new UserMapper();
            userMapper.ChangeCoinNumByUserId(1, coinItemDisplay.Value);
            // 将购买的卡包添加到玩家背包
            PackageMapper packageMapper = new PackageMapper();
            // 判断玩家是否已有购买相同卡包
            // TODO 修改为当前登录的玩家
            List<UserPackageVO> packages = packageMapper.GetPackageByUserIdAndPackageId(1 , curPurchasePackageId);
            if (packages != null&&packages.Count > 0)
            {
                UserPackageVO package = packages[0];
                // 进行修改操作
                // TODO 修改为当前登录的玩家
                packageMapper.UpdateUserPackageRelDataByPackageIdAndUserId(curPurchasePackageId, 1, package.PackageCount+1);
            }
            else
            {
                // 进行添加操作
                // TODO 修改为当前登录的玩家
                packageMapper.AddUserPackageRelData(curPurchasePackageId, 1, 1);
            }
        }

        private void ClearPackages()
        {
            for (int i = packageList.Count-1; i >= 0; i--)
            {
                Destroy(packageList[i]);
                packageList.RemoveAt(i);
            }
        }
    }
}
